package com.aaa.cs.service.impl;

import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnStatus;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.cs.dao.RoleDao;
import com.aaa.cs.entity.Role;
import com.aaa.cs.service.RoleService;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色信息表(Role)表服务实现类
 *
 * @author makejava
 * @since 2020-12-01 14:38:58
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleDao roleDao;

    /**
     * 通过ID查询单条数据
     *
     * @param roleId 主键
     * @return 实例对象
     */
    @Override
    public Role queryById(Integer roleId) {
        return this.roleDao.queryById(roleId);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param pageNo
     * @param pageSize
     * @param role     实例对象
     * @return 对象列表
     */
    @Override
    public Result queryAll(int pageNo, int pageSize, Role role) {
        if(pageNo==0||pageSize==0){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        //分页
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<Role> pageInfo = new PageInfo<Role>(roleDao.queryAll(role));
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), pageInfo);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Role> queryAllByLimit(int offset, int limit) {
        return this.roleDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param role 实例对象
     * @return 实例对象
     */
    @Override
    public Role insert(Role role) {
        this.roleDao.insert(role);
        return role;
    }

    /**
     * 修改数据
     *
     * @param role 实例对象
     * @return 实例对象
     */
    @Override
    public Role update(Role role) {
        this.roleDao.update(role);
        return this.queryById(role.getRoleId());
    }

    /**
     * 通过主键删除数据
     *
     * @param roleId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer roleId) {
        return this.roleDao.deleteById(roleId) > 0;
    }

    /**
     *
     * @param roleId
     * @return
     */
    @Override
    public List<Integer> listMenuByRoleId(int roleId) {
        return roleDao.listMenuByRoleId(roleId);
    }

    @Override
    public Map list(Map map) {
        if (!"null".equals("pageNo")&&!"null".equals("pageSize")) {
            int pageNo = Integer.valueOf(map.get("pageNo") + "");
            int pageSize = Integer.valueOf(map.get("pageSize") + "");
            PageHelper.startPage(pageNo, pageSize);
            PageInfo<Map> pageInfo = new PageInfo<Map>(roleDao.getList(map));
            Map rmap = new HashMap();
            rmap.put("total", pageInfo.getTotal());
            rmap.put("page", pageInfo.getList());
            return rmap;
        }
        return null;
    }

    @Override
    public int addRoleAndMenu(int roleId, String menuIds) {
        // 删除原有角色关联的所有权限
        int r1 = roleDao.deleteMenuByRoleId(roleId);
        System.out.println("获取到的菜单ID："+menuIds);
        int r2=0;
        //分割menuIds  1，2,3,100,101,103
        if(!StringUtils.isEmpty(menuIds)){
            //roleIdsAndMenuIds = (1,1),(1,2),(1,3),(1,100),....
            StringBuffer roleIdsAndMenuIds=new StringBuffer();
            String[] midArray = menuIds.split(",");
            //循环遍历
            for (String menuId : midArray) {
                roleIdsAndMenuIds.append("("+roleId+","+menuId+"),");
            }
            //去掉最后一个逗号
            String tmpStr=roleIdsAndMenuIds.substring(0,roleIdsAndMenuIds.length()-1);
            //执行添加
            r2 = roleDao.addRoleAndMenu(tmpStr);
        }
        //上面两个操作都成功，返回1
        if(r1>0&&r2>0)
            return 1;
        return 0;
    }
}